<extend name="Public/base" />
<block name="body">
  <div class="main-title cf">
    <h2>新增订货订单</h2>
  </div>
  
  <!-- 标签页导航 -->
  <div class="tab-wrap">
    <div class="tab-content"> 
      <!-- 表单 -->
      <form id="form" action="{:U('update')}" method="post" class="form-horizontal doc-modal-form">
        <!-- 基础 -->
        
        <div id="tab1" class="tab-pane in tab1">
          <div class="form-item cf">
            <label class="item-label">请选择商品</label>
            <div class="search-form  cf">
              <div class="sleft">
                <input type="text" name="title" class="search-input" value="{:I('title')}" placeholder="请输入商品名称查询">
                <a class="sch-btn" href="javascript:;" id="search" url="{:U('Public/goods')}"><i class="btn-search"></i></a> </div>
            </div>
            <div class="controls">
             	  <div class="fl">
              <select multiple="multiple" class="text input-large" name="goods_id" id="goods_id" style="height:120px" onchange="goods(this.options[this.options.selectedIndex].value, this.options[this.options.selectedIndex].text)">
                <volist name="_list" id="vo">
                  <option value="{$vo.id}">{$vo.title}</option>
                </volist>
              </select>
              </div>
              <div class="fl" style="margin-left:6px;">
               <label class="item-label">剩余库存</label>
               <input type="text" class="text input-mid" disabled="disabled" name="stock" id="stock" style="width:100px;" value="" placeholder="商品库存" >
              <input placeholder="商品价格"  type="text" class="text input-mid" name="price" id="price" style="width:100px;" disabled="disabled" value="" >
               <label class="item-label">订货数量</label>
               <input type="text" class="text input-mid" id="ordbuynum" name="ordbuynum" value="" style="width:100px;" placeholder="请输入订货数量">
               <button class="addgoods btn" id="buttongoods" names="" ids="">增+</button>
             </div>
            </div>
             <div class="controls">
             <label class="item-label">订货详情</label>
             <div id="order" style="background:#FFF; width:575px; padding:10px;"></div>
            </div>
          </div> 
          
          <div class="form-item">
            <label class="item-label">收货人信息<span class="check-tips"></span></label>
            <div class="controls">
              <input type="text" class="text" name="name" value="{$customer.name}" placeholder="收货人姓名" >
              <span class="check-tips">收货人姓名</span><br />
              <input type="text" class="text" name="phone" value="{$customer.phone}" placeholder="收货人手机号" >
              <span class="check-tips">收货人联系方法</span><br />
              <input type="text" class="text input-large" name="address" value="{$customer.address}" placeholder="收货人地址">
              <span class="check-tips">收货人地址</span></div>
          </div>
       
          <div class="form-item cf">
            <label class="item-label">订单描述<span class="check-tips"></span></label>
            <div class="controls">
              <label class="textarea input-large">
                <textarea name="mssegs"></textarea>
              </label>
            </div>
          </div>
        </div>
        <!-- 按钮 -->
        <div class="form-item cf">
          <div class="controls edit_sort_btn">
            <input type="hidden" name="id" value="{$info.id|default=0}" />
            <input type="hidden" name="cid" value="{$customer.id|default=0}" />
            <button class="btn ajax-post submit-btn  no-refresh" type="submit" target-form="form-horizontal">确 定</button>
            <button class="btn btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</block>
<block name="script"> 
  <script type="text/javascript" charset="utf-8">  
  	$("#search").click(function(){
		var url = $(this).attr('url');
        var query  = $('.search-form').find('input').serialize();
        query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
        query = query.replace(/^&/g,'');
		if(status != ''){
			query = 'status=' + status + "&" + query;
        }
        if( url.indexOf('?')>0 ){
            url += '&' + query;
        }else{
            url += '?' + query;
        }
		$.get(url, {},function(data){
			$('#goods_id').html(data);
		});
	});

		
	function goods(s, t){
		var url = '{:U('Public/goodsInfo')}';
		$.post(url, {id:s},function(data){
			$('#stock').val(data.stock);
			$('#price').val(data.price);
			$('#buttongoods').attr('ids', s);
			$('#buttongoods').attr('names', t);
		}, 'json');
	} 

	$('.addgoods').click(function(){

			var stock = $('#stock').val();
			var price = $('#price').val();
								
						
			var names = $(this).attr('names');
			var ids = $(this).attr('ids');	
			var ordbuynum = $('#ordbuynum').val();
			var goods_ids=$('input[name="goodsids[]"]');
			var f = false;
			$.each(goods_ids, function(i) {    
				var v = goods_ids[i].value;
				if (v == ids) {
					f = true;
					return false;
				}
			});
			if (f === true) {
				updateAlert('您选择的商品已存在订货详情！');
				setTimeout(function(){
					$('#top-alert').find('button').click();
				},1500);
				return false;
			}
			
			if (ids == '') {
				updateAlert('请选择订货商品！');
				setTimeout(function(){
					$('#top-alert').find('button').click();
				},1500);
				return false;
			}
			if (ordbuynum == '') {
				updateAlert('请输入订货数量！');
				setTimeout(function(){
					$('#top-alert').find('button').click();
				},1500);
				return false;
			}
			
			if (parseInt(ordbuynum) > parseInt(stock)) {
				updateAlert('库存不足');
				setTimeout(function(){
					$('#top-alert').find('button').click();
				},1500);
				return false;
			}
			
			var str = '<div style="margin-bottom:3px;" readonly="readonly" id="id_'+ids+'"><input type="text" class="text input-large" name="namesarr[]"  value="'+names+'" ><input type="hidden" class="text input-large" name="goodsids[]"  value="'+ids+'" ><input type="hidden" class="text input-large" name="pricearr[]"  value="'+price+'" ><input type="text" class="text input-mid" name="stockarr[]"  style="width:100px;"  value="'+ordbuynum+'" > <a class="btn" onclick=delgoods("'+ids+'") style="background-color: #cc3900;color: #fff;">减-</a></div>';
			$('#order').append(str);
			return false;	
	});
	
	
	function delgoods(ids){
		$('#id_'+ids).remove();
	}
    //导航高亮
 	highlight_subnav('{:U('Order/add')}');
</script> 
</block>
